package it.globalgamejam.scar.game 
{
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import it.globalgamejam.scar.components.CustomDraw;
	import it.globalgamejam.scar.game.world.Ball;
	import it.globalgamejam.scar.game.world.Catapult;
	import it.globalgamejam.scar.game.world.Goal;
	import it.globalgamejam.scar.game.world.Ground;
	import it.globalgamejam.scar.game.world.Mouse;
	import it.globalgamejam.scar.game.world.Player;
	import it.globalgamejam.scar.game.world.Rock;
	import it.globalgamejam.scar.game.world.Wall;
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Canvas;
	import net.flashpunk.graphics.Stamp;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.World;
	/**
	 * ...
	 * @author N1xx1
	 */
	public class Puzzle 
	{
		
		public static var puzzle1:Puzzle = new Puzzle([
			[101,105,105,105,105,105,105,102],
			[107,  1,  2,  0,  0,  0,  3,106],
			[103,108,108,108,108,108,108,104]
		]);
		
		public static var puzzle2:Puzzle = new Puzzle([
			[100,101,105,105,105,105,102],
			[100,107,  0,  0,  0,  0,106],
			[100,107,  2,100,100,  0,106],
			[100,107,  1,  0,  0,  0,106],
			[100,107,  0,100,100,  0,106],
			[107,  3,  0,  0,  0,  0,106],
			[103,108,108,108,108,108,104]
		]);
		
		public static var t13:Object = { "method": Mouse.createMouse, "parms": [1] };
		public static var t23:Object = { "method": Mouse.createMouse, "parms": [3] };
		public static var r13:Object = { "method": Rock.createRock, "parms": [] };
		public static var r23:Object = { "method": Rock.createRock, "parms": [] };
		public static var puzzle3:Puzzle = new Puzzle([
			[100,100,100,100,100,100,100],
			[100,100,  0,  0,r13,  0,100],
			[100,100,  0,r23,  2,  0,100],
			[100,100,  0,  0,100,100,100],
			[100,100,t13,100,100,100,100],
			[100,  0,  0,  0,  0,100,100],
			[100,  0,  0,  0,  0,100,100],
			[100,  0,100,100,t23,100,100],
			[100,  1,100,100,  3,100,100],
			[100,100,100,100,100,100,100]
		]);
		
		public static var cp4:Object = { "method": Catapult.createCatapult, "parms": [1] };
		public static var puzzle4:Puzzle = new Puzzle([
			[100,100,100,100,100,100,100,100],
			[100,  1,  2,cp4,  0,  0,  3,100],
			[100,100,100,100,100,100,100,100]
		]);
		
		private var matrix:Array;
		
		public function Puzzle(_matrix:Array) 
		{
			matrix = _matrix;
		}
		
		public function fillWorld(world:World):Player {
			var yMax:uint = matrix.length;
			var xMax:uint = matrix[0].length;
			
			var picture:Canvas = new Canvas(xMax * 120, yMax * 120);
			
			var pl:Player;
			for (var y:uint = 0; y < yMax; y++) {
				for (var x:uint = 0; x < xMax; x++) {
					if (matrix[y][x] is Number) {
						switch(matrix[y][x]) {
							case 1:
								pl = new Player(x * 120, y * 120);
								world.add(pl);
								break;
							case 2:
								world.add(new Ball(x * 120, y * 120));
								break;
							case 3:
								world.add(new Goal(x * 120, y * 120));
								break;
						}
						if (matrix[y][x] >= 100 && matrix[y][x] <= 120) {
							if (y == 0) picture.fill(new Rectangle(x * 120, 0, (x + 1) * 120, 1), 0x000000);
							if (x == 0) picture.fill(new Rectangle(0, y * 120, 1, (y + 1) * 120), 0x000000);
							if (y == yMax - 1) picture.fill(new Rectangle(x * 120, yMax * 120 - 1, 120, 1), 0x000000);
							if (x == xMax - 1) picture.fill(new Rectangle(xMax * 120 - 1, y * 120, 120, 1), 0x000000);
							
							if (y > 0) {
								var top:int = matrix[y-1][x];
								if (!(top <= 120 && top >= 100)) picture.fill(new Rectangle(x * 120, y * 120, 120, 4), 0x000000);
							}
							if (y < yMax - 1) {
								var bottom:int = matrix[y+1][x];
								if (!(bottom <= 120 && bottom >= 100)) picture.fill(new Rectangle(x * 120, (y + 1) * 120 - 4, 120, 4), 0x000000);
							}
							if (x > 0) {
								var left:int = matrix[y][x-1];
								if (!(left <= 120 && left >= 100)) picture.fill(new Rectangle(x * 120 - 4, y * 120, 4, 120), 0x000000);
							}
							if (x < xMax - 1) {
								var right:int = matrix[y][x+1];
								if (!(right <= 120 && right >= 100)) picture.fill(new Rectangle((x + 1) * 120, y * 120, 4, 120), 0x000000);
							}
							
							world.add(new Wall(matrix[y][x] - 100, x * 120, y * 120));
						}
					} else if(matrix[y][x] is Object) {
						var p:Entity = (matrix[y][x]["method"] as Function).apply(null, matrix[y][x]["parms"]);
						
						if ("setSpawnPos" in p) {
							p["setSpawnPos"](x * 120, y * 120);
						} else {
							p.x = x * 120;
							p.y = y * 120;
						}
						world.add(p);
					}
					world.add(new Ground(x * 120, y * 120));
				}
			}
			
			var borders:CustomDraw = new CustomDraw(0, 0, picture);
			world.add(borders);
			return pl;
		}
		
	}
}

